<template>
    <div class="login-wrap">
        <div class="ms-login">
            <div class="ms-title">后台管理系统</div>

            <el-form :model="param" :rules="rules" ref="login" label-width="0px" class="ms-content">
                <!-- 用户名 -->
                <el-form-item prop="username">
                    <el-input v-model="param.username" placeholder="username">
                        <el-button slot="prepend" icon="el-icon-lx-people">
                            &nbsp;用&nbsp;户&nbsp;名
                        </el-button>
                    </el-input>
                </el-form-item>

                <!-- 密码 -->
                <el-form-item prop="password_one">
                    <el-input
                        type="password_one"
                        placeholder="password_one"
                        v-model="param.password_one"
                        @keyup.enter.native="submitForm()"
                    >
                        <el-button slot="prepend" icon="el-icon-lx-lock">
                            &nbsp;密&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;码
                        </el-button>
                    </el-input>
                </el-form-item>

                <!-- 确认密码 -->
                <el-form-item prop="password_two">
                    <el-input
                        type="password_two"
                        placeholder="password_two"
                        v-model="param.password_two"
                        @keyup.enter.native="submitForm()"
                    >
                        <el-button slot="prepend" icon="el-icon-lx-lock">&nbsp;确认密码</el-button>
                    </el-input>
                </el-form-item>

                <!-- 邮箱 -->
                <el-form-item prop="email">
                    <el-input v-model="param.email" placeholder="email">
                        <el-button slot="prepend" icon="el-icon-lx-people">
                                &nbsp;邮&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;箱
                        </el-button>
                    </el-input>
                </el-form-item>

                <!-- 手机号码 -->
                <el-form-item prop="phone">
                    <el-input v-model="param.phone" placeholder="phone">
                        <el-button slot="prepend" icon="el-icon-lx-people">
                            &nbsp;手机号码
                        </el-button>
                    </el-input>
                </el-form-item>

                <!-- 选择注册权限 -->
                <el-form-item>
                    
                    <el-select v-model="value" placeholder="请选择注册权限" style="width: 290px;">
                       <el-option
                            v-for="item in options"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>

                <!-- 选择所属公司 -->
                <el-form-item>
                    <el-select v-model="companies_id" placeholder="请选择所属公司" style="width: 290px;" >
                        <el-option
                            v-for="a in companies_json"
                            :key="a.value"
                            :label="a.label"
                            :value="a.value">
                        </el-option>
                    </el-select>
                </el-form-item>

                <!--验证码输入 -->
                <el-form-item prop="code">
                    <el-input v-model="param.code" placeholder="code">
                        <el-button slot="prepend" icon="el-icon-lx-people">
                                &nbsp;验&nbsp;证&nbsp;码
                        </el-button>
                    </el-input>
                </el-form-item>
                <center>
                    <img :src="src" @click="changeimg">
                </center>
                <div class="login-btn">
                    <el-button type="primary" @click="submitForm()">注册</el-button>
                </div>

            </el-form>
        </div>
    </div>
</template>

<script>
import { register } from "../../api/api.js";


export default {
    data: function() {
        var validatepass = (rule,value,callback) => {
            if (value === ""){
                callback(new Error('请在次输入密码'));
            }else if(value !== this.param.password_one){
                callback(new Error('两次输入的密码不一致'));
            }else{
                callback();
            }
        };
        var validatephone = (rule,value,callback) => {
            if (value === ""){
                callback(new Error('请输入手机号'));
            }else if(!/^((1[3,5,8][0-9])|(14[5,7])|(17[0,6,7,8])|(19[7]))\d{8}$/.test(value)){
                callback(new Error('请输入正确的手机号'));
            }else{
                callback();
            }
        };

        return {
            src:'http://localhost:8000/mycode/',
            param: {
                username: this.username,
                password_one: this.password_one,
                password_two:this.password_two,
                email:this.email,
                phone:this.phone,
                juris:this.value,
                identity:'',
                is_activate:0,
                companies:this.companies_id,
                code:this.code,

            },
            
            rules: {
                username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
                password_one: [{ required: true, message: '请输入密码', trigger: 'blur' }],
                password_two: [{ required: true, validator: 'validatepass',trigger: 'blur' }],
                email: [{ required: true, message: '请输入邮箱', trigger: 'blur' }],
                phone: [{ required: true, validator: 'validatephone',trigger: 'blur' }],
                code: [{ required: true, message: '请输入验证码', trigger: 'blur' }],
            },

            //选择注册权限
            options: [{
                value: '2',
                label: '管理员'
            }, {
                value: '3',
                label: '发货员'
            },{
                value: '4',
                label: '收货员'
            },{
                value: '5',
                label: '客服'
            },],
            value: '',

            //选择所属公司
            companies_json: [{
                value: '1',
                label: '寰宇集团'
            }, {
                value: '2',
                label: '喵喵测'
            },{
                value: '3',
                label: '跃达石材加工厂'
            }],
            companies_id:'',

        };
    },

    methods: {
        submitForm(){
            var params = {
                username: this.param.username,
                password_one: this.param.password_one,
                password_two:this.param.password_two,
                email:this.param.email,
                phone:this.param.phone,
                juris:this.value,
                identity:this.value,
                is_activate:this.param.is_activate,
                companies:this.companies_id,
                code:this.param.code,

            }
            register(params).then(res => {
                this.$message.success(res.message);
                alert('注册成功');
                
            })
            this.$router.push({path:'/login'})
        },

        //刷新验证码
        changeimg:function(){
            //随机字符串
            var num = Math.ceil(Math.random()*100)
            //赋值地址
            this.src = this.src + '?code=' + num;
        }
    },
};
</script>

<style scoped>
.login-wrap {
    position: relative;
    width: 100%;
    height: 100%;
    background-image: url(../../assets/img/login-bg.jpg);
    background-size: 100%;
}
.ms-title {
    width: 100%;
    line-height: 50px;
    text-align: center;
    font-size: 20px;
    color: #fff;
    border-bottom: 1px solid #ddd;
}
.ms-login {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 350px;
    margin: -190px 0 0 -175px;
    border-radius: 5px;
    background: rgba(255, 255, 255, 0.3);
    overflow: hidden;
}
.ms-content {
    padding: 30px 30px;
}
.login-btn {
    text-align: center;
}
.login-btn button {
    width: 100%;
    height: 36px;
    margin-bottom: 10px;
}
.login-tips {
    font-size: 12px;
    line-height: 30px;
    color: #fff;
}
</style>